﻿var modalAddProject;
$().ready(function () {
    modalAddProject = new ModalAddProject();
    modalAddProject.Initialize();
});
function ModalAddProject() {
    var _this = this;
    var ajaxUrl = "/Manager/CreateNewTask";
    var developerajaxUrl = "/Manager/DeveloperWithProject";
    var dialogName = "#modalAddTask";

    this.Initialize = function () {
        _this.dialogInstance().dialog({
            autoOpen: false,
            width: "435px",
            resizable: false,
            modal: true,

            buttons: {
                "Add": function () {
                    _this.execute();
                    $(this).dialog("close");
                },
                "Cancel": function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#addReport").click(function () {
            _this.open();
        });

        $("#project").change(function () {
            _this.changeDeveloper();
        });
    }

    this.dialogInstance = function () {
        return $(dialogName);
    }
    this.open = function () {
        $("#developer", _this.dialogInstance()).attr("disabled", true);
        _this.dialogInstance().dialog("open");
    }
    this.close = function () {
        _this.dialogInstance().dialog("close");
    }
    this.execute = function () {
        var dataAjax = {
            Title: $("#title", _this.dialogInstance()).attr("value"),
            Decsriprion: $("#description", _this.dialogInstance()).attr("value"),
            ProjectID: $("#project", _this.dialogInstance()).attr("value"),
            DeveloperID: $("#developer", _this.dialogInstance()).attr("value")
        };

        $.ajax(
        {
            type: "POST",
            traditional: true,
            url: ajaxUrl,
            data: dataAjax,
            success: function (data) {
                if (data.result == true) {
                    window.location.replace(document.location.href);
                }
                else {
                }
            },
            error: function () {
                alert("Ошибка");
            }
        });
    }

    this.changeDeveloper = function () {

        var item = $("#project", _this.dialogInstance()).attr("value");
        if (item != "") {
            var dataAjax = {
                projctID: item
            };

            $.ajax(
            {
                type: "POST",
                traditional: true,
                url: developerajaxUrl,
                data: dataAjax,
                success: function (data) {
                    if (data.result == true) {
                        _this.developer(data.data)
                    }
                    else {

                    }
                },
                error: function () {
                    alert("Ошибка");
                }
            });
        }
        else {
            $('#developer >option').remove();
            $("#developer", _this.dialogInstance()).attr("disabled", true);
        }
    }

    this.developer = function (data) {
        if (data != null && data.length != 0) {
            $('#developer >option').remove();
            $("#developer", _this.dialogInstance()).attr("disabled", false);
            for (var i = 0; i < data.length; i++) {
                $("#developer").prepend('<option value="' + data[i].Value + '">' + data[i].Text + '</option>');
            }
        }
        else {
            $('#developer >option').remove();
            $("#developer", _this.dialogInstance()).attr("disabled", true);
        }
    }
}